﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WhsFacebook.Net.Web.Facebook.Handlers;

namespace WhsFacebook.Net.Web.Facebook
{
    /// <summary>
    /// Retrieves an add-in image
    /// </summary>
    public class AddinImage : SafeHttpHandler
    {

        protected override void ProcessRequestSafe(HttpContext context)
        {
            // parse data
            var addinProductCode = context.Request.Params["addin"];

            if (string.IsNullOrEmpty(addinProductCode))
                throw new ArgumentException("add-in product code not specified: missing 'addin' query parameter", "addin");

            Guid addinProductCodeParsed;

            if (!Guid.TryParse(addinProductCode, out addinProductCodeParsed))
                throw new ArgumentException("add-in product code invalid: failed to parse 'addin' query parameter", "addin");

            if (addinProductCodeParsed == Guid.Empty)
                throw new ArgumentException("add-in product code invalid: GUID in 'addin' query parameter is Guid.Empty", "addin");

            // query image url from db (cache) first
            using (var client = new Database.WhsFacebookEntities())
            {
                var addinId = (
                    from a in client.WhsAddins
                    where a.ProductCode == addinProductCodeParsed
                    && a.HslAddinId != null
                    select a.HslAddinId
                    ).FirstOrDefault();

                if (addinId == null)
                    throw new ArgumentException("no addin found for product code: " + addinProductCodeParsed, "addin");

                var screenshot = (
                    from s in client.WhsScreenshots
                    where s.HslAddinId == addinId
                    orderby s.IsDefault descending
                    select s
                    ).FirstOrDefault();

                if (screenshot != null)
                {
                    // redirect to screenshot then
                    context.Response.Redirect(screenshot.ImageUrl);

                    return;
                }
            }

            // query image url from service
            using (var client = new HomeServerLand.HSLServicesSoapClient())
            {
                var addinId = client.GetAddinByProductCode(addinProductCodeParsed);

                if (addinId == null)
                    throw new ArgumentException("no addin found for product code: " + addinProductCodeParsed, "addin");

                var screenshot = client.GetAddinScreenshotDefault(addinId.AddinID);

                if (screenshot == null)
                    throw new ArgumentException("no default screenshot found for add-in: " + addinProductCodeParsed, "addin");

                // redirect to screenshot then
                context.Response.Redirect(screenshot.PhotoURL);
            }
        }

    }
}